﻿using ChoMy.Areas.Admin.Models;
using ChoMy.Constants;
using ChoMy.Helpers;
using ChoMy.Models;
using ChoMy.Models.Data;
using PagedList;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Globalization;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using System.Web.Mvc;

namespace ChoMy.Areas.Admin.Controllers
{
    public class EventController : Controller
    {
        private ChoMyContext db = new ChoMyContext();
        private StringHelper stringHelper = new StringHelper();
        private FileHelper fileHelper = new FileHelper();
        private string dateRangeFormat = "dd/MM/yyyy hh:mm tt";
        private string title = "sự kiện";

        public EventController()
        {
            ViewBag.Title = title;
        }

        [Authorize(Roles = "Admin, Read Event")]
        public ActionResult Index(int? categoryId, bool? isTrash, string search, string sortOrder, int? page, int? pageSize)
        {
            var sortList = new List<string> { "Name", "EndDate", "BeginDate", "Category.Name", "DateModified", "DateCreated", "EventProducts.Count" };
            var pageSizeList = new List<int> { 10, 25, 50, 100 };
            int pageNumber = page ?? 1;

            pageSize = pageSize ?? 10;
            sortOrder = sortOrder ?? "DateCreated_desc";
            isTrash = isTrash ?? false;

            ViewBag.IsTrash = isTrash;
            ViewBag.PageSize = pageSize;
            ViewBag.PageSizeDDL = new SelectList(pageSizeList, pageSize);
            ViewBag.ReturnListUrl = Request.Url.AbsoluteUri;

            IQueryable<Event> query = db.Events;

            ViewBag.TrashCount = query.Count(x => x.IsDelete);
            ViewBag.ListCount = query.Count(x => !x.IsDelete);

            query = CategoryFilter(query, categoryId);          

            if (!String.IsNullOrWhiteSpace(search))
            {
                page = 1;

                query = Search(query, search.Trim());
            }

            query = Sort(query, sortOrder, sortList);

            query = query.Where(x => x.IsDelete == isTrash);

            return View(query.ToPagedList(pageNumber, (int)pageSize));
        }

        [Authorize(Roles = "Admin, Read Event")]
        public ActionResult Details(int id, string partialView = "_Details")
        {
            Event eventt = db.Events.Find(id);

            return PartialView(partialView, eventt);
        }

        [Authorize(Roles = "Admin, Create Event")]
        public ActionResult Create(string returnListUrl)
        {
            ViewBag.CategoryDDL = CategorySelectList(0);
            ViewBag.ReturnListUrl = returnListUrl;

            return View();
        }

        [HttpPost]
        [Authorize(Roles = "Admin, Create Event")]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Create(EventCreator model, string returnListUrl)
        {
            if (ModelState.IsValid)
            {
                List<DateTime> dateTimeList = model.Deadline.Split('-')
                    .Select(x => DateTime.ParseExact(x.Trim(), dateRangeFormat, CultureInfo.InvariantCulture)).ToList();

                Event eventt = new Event();
                eventt.Name = model.Name.Trim();
                eventt.CategoryId = model.CategoryId;
                eventt.Description = model.Description;
                eventt.BeginDate = dateTimeList[0];
                eventt.EndDate = dateTimeList[1];

                if (String.IsNullOrWhiteSpace(fileHelper.Valid(model.Image)))
                {
                    eventt.ImageUrl = fileHelper.Upload(model.Image, eventt.Name, "/upload/image/event");
                }

                db.Events.Add(eventt);
                db.SaveChanges();

                SaveHistory("Create", eventt.Name);
                AlertHandling("Create", eventt.Name);

                if (!String.IsNullOrWhiteSpace(returnListUrl))
                {
                    return Redirect(returnListUrl);
                }

                return RedirectToAction("Index");
            }

            ViewBag.CategoryDDL = CategorySelectList(0);
            ViewBag.ReturnListUrl = returnListUrl;

            return View(model);
        }

        [Authorize(Roles = "Admin, Edit Event")]
        public ActionResult Edit(int? id, string returnListUrl)
        {
            if (id == null)
            {
                return RedirectToAction("BadRequest", "Error");
            }

            Event eventt = db.Events.Find(id);

            if (eventt == null)
            {
                return RedirectToAction("NotFound", "Error");
            }

            var model = new EventEditor();
            model.Id = eventt.Id;
            model.Name = eventt.Name;
            model.Description = eventt.Description;
            model.Deadline = eventt.BeginDate.ToString(dateRangeFormat) + " - " +
                eventt.EndDate.ToString(dateRangeFormat);
            model.CategoryId = (int)eventt.CategoryId;

            ViewBag.CategoryDDL = CategorySelectList(model.CategoryId);
            ViewBag.ReturnListUrl = returnListUrl;

            return View(model);
        }

        [HttpPost]
        [Authorize(Roles = "Admin, Edit Event")]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Edit(EventEditor model, string returnListUrl)
        {
            if (ModelState.IsValid)
            {
                List<DateTime> dateTimeList = model.Deadline.Split('-')
                    .Select(x => DateTime.ParseExact(x.Trim(), dateRangeFormat, CultureInfo.InvariantCulture)).ToList();

                Event eventt = db.Events.Find(model.Id);
                eventt.Name = model.Name.Trim();
                eventt.CategoryId = model.CategoryId;
                eventt.Description = model.Description;
                eventt.BeginDate = dateTimeList[0];
                eventt.EndDate = dateTimeList[1];
                eventt.DateModified = DateTime.Now;
                eventt.ModifiedBy = User.Identity.Name;

                if (String.IsNullOrWhiteSpace(fileHelper.Valid(model.Image)))
                {
                    fileHelper.Delete(eventt.ImageUrl);
                    eventt.ImageUrl = fileHelper.Upload(model.Image, eventt.Name, "/upload/image/event");
                }

                db.Entry(eventt).State = EntityState.Modified;
                db.SaveChanges();

                SaveHistory("Edit", eventt.Name);
                AlertHandling("Edit", eventt.Name);

                if (!String.IsNullOrWhiteSpace(returnListUrl))
                {
                    return Redirect(returnListUrl);
                }

                return RedirectToAction("Index");
            }

            ViewBag.CategoryDDL = CategorySelectList(model.CategoryId);
            ViewBag.ReturnListUrl = returnListUrl;

            return View(model);
        }

        [HttpPost]
        [Authorize(Roles = "Admin, Delete Without Recycle Event")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteWithoutRecycle(int id, string returnListUrl)
        {
            Event eventt = db.Events.Find(id);

            string eventName = eventt.Name;

            fileHelper.Delete(eventt.ImageUrl);

            db.Events.Remove(eventt);
            db.SaveChanges();

            SaveHistory("DeleteWithoutRecycle", eventName);
            AlertHandling("DeleteWithoutRecycle", eventName);

            if (!String.IsNullOrWhiteSpace(returnListUrl))
            {
                return Redirect(returnListUrl);
            }

            return RedirectToAction("Index", new { isTrash = true });
        }

        [HttpPost]
        [Authorize(Roles = "Admin, Delete Or Restore Event")]
        public ActionResult DeleteOrRestore(int id)
        {
            Event eventt = db.Events.Find(id);

            eventt.IsDelete = eventt.IsDelete ? false : true;
            eventt.DateModified = DateTime.Now;
            eventt.ModifiedBy = User.Identity.Name;

            db.Entry(eventt).State = EntityState.Modified;
            db.SaveChanges();

            if (eventt.IsDelete)
            {
                SaveHistory("Delete", eventt.Name);
            }
            else
            {
                SaveHistory("Restore", eventt.Name);
            }

            return Json(eventt.IsDelete ? 1 : 0, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult CheckName(string name, int? id)
        {
            name = name.Trim();

            Event eventt = db.Events.FirstOrDefault(x => x.Name == name && x.Id != id);

            if (eventt != null)
            {
                if (!eventt.IsDelete)
                {
                    string url = Url.Action("Index", new { search = name, isTrash = false });
                    string result = String.Format("Tên {0} này đã tồn tại. <a href='{1}'>Chi tiết</a>.", title, url);

                    return Json(result, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    string url = Url.Action("Index", new { search = name, isTrash = true });
                    string result = String.Format("Tên {0} này đã tạm thời bị xóa. <a href='{1}'>Chi tiết</a>.", title, url);

                    return Json(result, JsonRequestBehavior.AllowGet);
                }
            }

            return Json(true, JsonRequestBehavior.AllowGet);
        }

        #region Helpers
        private SelectList CategorySelectList(int id)
        {
            IQueryable<Category> categories = db.Categories
                .Where(x => x.ParentId == null && !x.IsDelete);

            return new SelectList(categories, "Id", "Name", id);
        }

        private void SaveHistory(string crud, string name)
        {
            string type = "Event";

            DateTime dateTime = DateTime.Now;
            string userName = User.Identity.Name;
            string description = "";

            switch (crud)
            {
                case "Create":
                    description = String.Format("<b>{0}</b> tạo mới {1} <b>{2}</b>", userName, title, name);
                    break;
                case "Edit":
                    description = String.Format("<b>{0}</b> chỉnh sửa {1} <b>{2}</b>", userName, title, name);
                    break;
                case "DeleteWithoutRecycle":
                    description = String.Format("<b>{0}</b> xóa vĩnh viễn {1} <b>{2}</b>", userName, title, name);
                    break;
                case "Delete":
                    description = String.Format("<b>{0}</b> xóa tạm thời {1} <b>{2}</b>", userName, title, name);
                    break;
                case "Restore":
                    description = String.Format("<b>{0}</b> khôi phục {1} <b>{2}</b>", userName, title, name);
                    break;
            }

            var history = new History();
            history.DateTime = dateTime;
            history.Description = description;
            history.HistoryTypeId = db.HistoryTypes.First(x => x.Name == type).Id;

            db.Histories.Add(history);
            db.SaveChanges();
        }

        private void AlertHandling(string crud, string name)
        {
            string userName = User.Identity.Name;
            string alertMessage = "";

            switch (crud)
            {
                case "Create":
                    alertMessage = String.Format("Bạn vừa tạo mới {0}: <b>{1}</b>", title, name);
                    break;
                case "Edit":
                    alertMessage = String.Format("Bạn vừa chỉnh sửa {0}: <b>{1}</b>", title, name);
                    break;
                case "DeleteWithoutRecycle":
                    alertMessage = String.Format("Bạn vừa xóa vĩnh viễn {0}: <b>{1}</b>", title, name);
                    break;
            }

            if (!String.IsNullOrWhiteSpace(alertMessage))
            {
                Session["alert-message-" + userName] = alertMessage;
                Session["alert-type-" + userName] = "alert-success";
            }
        }

        private IQueryable<Event> CategoryFilter(IQueryable<Event> query, int? categoryId)
        {
            List<Category> categories = query
               .Where(x => !x.IsDelete)
               .GroupBy(x => x.Category)
               .Select(x => x.Key)
               .OrderBy(x => x.Name)
               .ToList();

            ViewBag.CategoryDDL = new SelectList(categories, "Id", "Name", categoryId);

            if (categoryId != null)
            {
                ViewBag.CategoryId = categoryId;

                query = query.Where(x => x.CategoryId == categoryId);
            }

            return query;
        }

        private IQueryable<Event> Search(IQueryable<Event> query, string searchString)
        {
            ViewBag.Search = searchString;

            query = query.Where(x => x.Name.Contains(searchString));

            return query;
        }

        private IQueryable<Event> Sort(IQueryable<Event> query, string sortOrder, List<string> sortList)
        {
            ViewBag.SortOrder = sortOrder;

            foreach (string sort in sortList)
            {
                ViewData[sort + "Sort"] = sortOrder == sort + "_desc" ? sort : sort + "_desc";
            }

            if (sortOrder.Contains("_desc"))
            {
                sortOrder = sortOrder.Replace("_desc", "");
                query = query.OrderBy(sortOrder + " descending");
                ViewData[sortOrder + "Icon"] = DataTableConstant.DescSortIcon;
            }
            else
            {
                query = query.OrderBy(sortOrder);
                ViewData[sortOrder + "Icon"] = DataTableConstant.AscSortIcon;
            }

            return query;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }

            base.Dispose(disposing);
        }
        #endregion
    }
}